﻿using com.iot.core.boot;
using com.iot.core.infrastructure;
using com.iot.core.util;
using com.iot.ht.Controllers.httask.service;
using NLog.Fluent;
using System.Data;

namespace com.iot.ht.httask.yjlxyj
{
    public class Server : com.iot.ht.Controllers.httask.service.HtAbstractTask
    {

        //P1 连接字符串

        DateTime ZXYJDT;
        public async override Task<MyApiResult> RunAsync(CancellationToken stoppingToken)
        {
            var r = new MyApiResult();
            if (ZXYJDT.Year < 2023)
            {
                try
                {
                    ZXYJDT = DateTime.Now;
                    var s = await Singleton<HtTaskService>.Instance.GetRunFile(HtTaskAppStateDto, "dt");
                    //Singleton<HtTaskService>.Instance.AddLog(this.HtTaskAppStateDto.HtTaskApp, $"readfile:{s}");

                    if ("" == s)
                    {

                    }
                    else
                    {
                        ZXYJDT = DateTime.Parse(s);
                    }
                }
                catch (Exception e)
                {
                }
            }


            var dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:00:00"));
 
            if (ZXYJDT != dt)
            {
               // Singleton<HtTaskService>.Instance.AddLog(this.HtTaskAppStateDto.HtTaskApp, $"ZXYJDT:{ZXYJDT}  dt:{dt}");
                var sql = string.Format("exec UPDL50_ZXYJ_SUM '{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));
                await SqlServerHelper.ExecuteNonqueryAsync(HtTaskAppStateDto.HtTaskApp.P1, sql);

                sql = string.Format("exec UPDL50_ZXYJ_YDFX '{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));
                await SqlServerHelper.ExecuteNonqueryAsync(HtTaskAppStateDto.HtTaskApp.P1, sql);

                sql = string.Format(@"select d.*,
	CASE WHEN D.ID='88' THEN '环保工况'  
	when  p.model is not null  and  p.model!='' then p.name+'('+p.model+')'
	else p.name
	END AS pname,
    isnull(p.model,p.name) as model
	 from ZXYJ_YJCFG  d  with(nolock) 
left join ZXProduct p on d.id=p.ID
where  d.zt!='正常'
if @@Rowcount>0
SELECT mobile FROM ZXYJ_USER where state='启用'
");
                var dst = SqlServerHelper.Query(HtTaskAppStateDto.HtTaskApp.P1, sql);
                var tb1 = dst.Tables[0];

                if (tb1.Rows.Count > 0)
                {
                    var mobiles = "";
                    foreach (DataRow dr in dst.Tables[1].Rows)
                    {
                        mobiles += dr[0].ToString() + ",";
                    }
                    mobiles = mobiles.TrimEnd(',');
                    if ("" != mobiles)
                    {
                        foreach (DataRow dr in tb1.Rows)
                        {
                            var msg = $"{dr["pname"]} {dr["zt"]}  离线：{dr["lx"]}/{dr["ZS"]} 异动：{dr["yds"]}({dr["ydl"]})";
                            await Singleton<HtTaskService>.Instance.SendSms(mobiles, msg);
                        }
                    }
                }
                ZXYJDT = dt;
                r.Msg = "时间：" + dt.ToString(DateUtil.DateFormatString);
                await Singleton<HtTaskService>.Instance.SaveRunFileAsync(HtTaskAppStateDto, "dt", ZXYJDT.ToString(DateUtil.DateFormatString));
            }
            else
            {
                r.Msg = "未到执行时间";              
            }
            return r;
        }
    }
}